package com.randi.monitor.controller;

import com.randi.common.core.domain.Result;
import com.randi.monitor.dto.SysCache;
import com.randi.monitor.service.ISysCacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author ziye
 */
@RestController
@RequestMapping("monitor/cache")
@Api(tags = "缓存监控")
public class SysCacheController {

    @Resource
    private ISysCacheService iSysCacheService;

    /**
     * 缓存信息
     *
     * @return Result
     */
    @GetMapping("/info")
    @ApiOperation(value = "缓存信息", notes = "")
    public Result getCacheInfo() {
        return Result.success().put(iSysCacheService.getCacheInfo());
    }

    /**
     * 缓存列表
     *
     * @return Result
     */
    @GetMapping("/names")
    @ApiOperation(value = "缓存列表", notes = "")
    public Result getCacheNames() {
        return Result.success().put(iSysCacheService.getCacheNames());
    }

    /**
     * 缓存键名列表
     *
     * @param sysCache 缓存信息
     * @return 缓存键名列表
     */
    @PostMapping("/keys")
    @ApiOperation(value = "缓存键名列表", notes = "")
    public Result getCacheKeys(@RequestBody SysCache sysCache) {
        return Result.success().put(iSysCacheService.getCacheKeys(sysCache));
    }

    /**
     * 缓存键名列表
     *
     * @return Result
     */
    @GetMapping("/detail/{cacheKey}")
    @ApiOperation(value = "缓存键值", notes = "")
    public Result getCacheValue(@PathVariable String cacheKey) {
        return Result.success().put(iSysCacheService.getCache(cacheKey));
    }

    /**
     * 清空缓存
     *
     * @param cacheName 缓存名称
     * @return Result
     */
    @GetMapping("/clear/name_{cacheName}")
    @ApiOperation(value = "清空缓存", notes = "")
    public Result clearCacheName(@PathVariable String cacheName) {
        iSysCacheService.clearCacheName(cacheName);
        return Result.success();
    }

    /**
     * 清除缓存
     *
     * @param cacheKey 缓存键名
     * @return Result
     */
    @GetMapping("/clear/key_{cacheKey}")
    @ApiOperation(value = "清除缓存", notes = "")
    public Result clearCacheKey(@PathVariable String cacheKey) {
        iSysCacheService.clearCacheKey(cacheKey);
        return Result.success();
    }

}
